package org.vacoor.ticket.handler.impl;

import java.io.UnsupportedEncodingException;

import org.vacoor.ticket.Constant;
import org.vacoor.ticket.handler.ResultHandler;
import org.vacoor.ticket.util.ParseUtil;

public class CheckOrderHandler implements ResultHandler<String>{

	/**
	 * 处理服务器返回如下的字符串
	 * {"checkHuimd":"Y","check608":"Y","msg":"","errMsg":"Y"}
	 *  当errMsg 不是Y 时: errMsg为错误消息,eg:系统忙，请稍后再试！
	 *  errMsg Y 表示基本成功,应该检查以下两个是否成功:
	 *      checkHuimd 为 N 则表示取消次数过多,今天不能受理订票请求
	 *      check608 为 N 则表示本车实行实名制,一日一车一证一票制
	 *  @return 返回"Y"表示成功或错误信息
	 */
	@Override
	public String handler(byte[] content) {
		String json;
		try {
			json = new String(content,Constant.ENCODING);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			json = new String(content);
		}
		String errMsg = ParseUtil.getValueFromJsonStr("errMsg", json);
		String more = ParseUtil.getValueFromJsonStr("checkHuimd", json);
		String bought = ParseUtil.getValueFromJsonStr("check608", json);
		String tmsg = ParseUtil.getValueFromJsonStr("msg", json);
		String msg = "";
		if(!"Y".equalsIgnoreCase(errMsg)){
			msg = errMsg;
		}else if ("N".equalsIgnoreCase(more)) {
			msg = "您取消次数太多,今天不能受理订票请求(msg:" + tmsg + ")";
		}else if ("N".equalsIgnoreCase(bought)) {
			msg = "本车实行实名制,一日一车一证一票制,请检查是否已经购买过今日票(msg:" + tmsg + ")";
		}else {
			msg = "Y";
		}
		return msg;
	}
	
}
